# Create your views here.
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponse, HttpResponseRedirect
from mysite.users.models import LoginForm, RequestForm, User
from django.forms.util import ErrorList
from django import forms
import hashlib
from time import time
from datetime import datetime

def login(request):	
	if request.method == 'POST':
		form = LoginForm(request.POST)
		if form.is_valid(): 
			email = form.cleaned_data['email']
			password = form.cleaned_data['password']
			
			wrong_entry = "Sorry, the username/password you tried is incorrect."
			try:
				user = User.objects.get(email=email)
			except User.DoesNotExist:
				return render_to_response('html/index_view.html', { 'wrong_entry': wrong_entry, 'form': form}, context_instance=RequestContext(request))
			else:
				hashing = hashlib.sha512(password).hexdigest()
				if hashing == user._hash:
					if user.activated == True:
						return render_to_response('html/home.html', { 'first': user.first_name }, context_instance=RequestContext(request))
					else:
						return render_to_response('html/sorry.html', {'problem': "account has not been activated by an administrator yet" }, context_instance=RequestContext(request))
				else:
					return render_to_response('html/sorry.html', { 'problem': "password is not accurate" }, context_instance=RequestContext(request))
	else:
		form = LoginForm() # An unbound form
		
	return render_to_response('html/index_view.html', {
		'form': form,
	}, context_instance=RequestContext(request))
	
def request_form(request):
	if request.method == 'POST':
		form = RequestForm(request.POST, error_class=DivErrorList)
		if form.is_valid():
			first_name = form.cleaned_data['first_name']
			last_name = form.cleaned_data['last_name']
			email = form.cleaned_data['email']
			password = form.cleaned_data['password']
			confirm_password = form.cleaned_data['confirm_password']
			
			if password == confirm_password:
				hexhash = hashlib.sha512(password).hexdigest()
				t = datetime.utcfromtimestamp(time())
				u = User(first_name=first_name, last_name=last_name, email=email, _hash=hexhash, timestamp=t, activated=False)
				u.save()
				return HttpResponseRedirect('/submitted')
			else:
				render_to_response('html/request_form.html', { 'form': RequestForm(request.POST, error_class=DivErrorList), 'problem': "Sorry, your passwords did not match."})
	else:
		form = RequestForm()
		
	return render_to_response('html/request_form.html', {
		'form': form,
	}, context_instance=RequestContext(request))

class DivErrorList(ErrorList):
	def __unicode__(self):
		return self.as_divs()
	def as_divs(self):
		if not self: return u''
		return u'%s' % ''.join([u'%s' %e for e in self])

def create_user(request):
	first_name = "John"
	last_name = "Sheridan"
	email = "john.sheridan4579@gmail.com"
	hexhash = hashlib.sha512("saltman2").hexdigest()
	t = datetime.utcfromtimestamp(time())

	u = User(first_name=first_name, last_name=last_name, email=email, _hash=hexhash, timestamp=t, activated=True)
	u.save()
	return HttpResponse("<html><body><center><h1>John has been created (hopefully!)</h1></center></body></html>")